/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.data.provider;

import java.nio.charset.Charset;
import java.util.List;

import chunmap.data.feature.Feature;
import chunmap.data.feature.FeatureCollection;
import chunmap.data.feature.LayerMetadata;
import chunmap.data.feature.ShapeFeature;
import chunmap.data.feature.VisitAction;
import chunmap.data.index.FeatureList;
import chunmap.data.provider.postgis.PostgisConnection;
import chunmap.data.provider.postgis.PostgisDataSource;
import chunmap.data.provider.shp.ShpDataSource;
import chunmap.model.elem.EnvelopeBuilder;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryType;
import chunmap.view.layer.VectorLayer;

/**
 * 地图数据工厂
 * @author chunquedong
 * 
 */
public class LayerFactory {
	public static VectorLayer openShapeFile(String path)
    {
		ShpDataSource pDR = new ShpDataSource(path);
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);

        FeatureCollection fc = pDA.createFeatureList();
        pDR.close();
        return new VectorLayer(fc);
    }
    public static VectorLayer openShapeFile(String path, final int labelColum)
    {
    	final ShpDataSource pDR = new ShpDataSource(path,Charset.forName("GBK"));
    	//System.out.print(Charset.defaultCharset());
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);

        FeatureCollection fc = pDA.createFeatureList();
        fc.each(new VisitAction(){
			@Override
			public void execute(Feature obj) {
				ShapeFeature shp = (ShapeFeature)obj;
				Object[] data=pDR.data(shp.getId());
				shp.setValues(data);
	            String s = data[labelColum].toString();
	            shp.setLabel(s);
			}});
        fc.setFeatureSchama(pDR.getFeatureSchama());
        pDR.close();
        return new VectorLayer(fc);
    }

    public static VectorLayer OpenPostGIS(String tableName
        , PostgisConnection ConnectionString, String geom, String id)
    {
    	PostgisDataSource pDR = new PostgisDataSource(tableName , geom, id,ConnectionString);
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);
        VectorLayer layer = new VectorLayer(pDA.createFeatureList());
        pDR.close();
        return layer;
    }

    //------------------------------------------------------------------------

    public static VectorLayer createGeometryLayer(List<Geometry> gs)
    {
        if (gs.size() == 0) return null;

        FeatureCollection fc = new FeatureList();
        EnvelopeBuilder eb = new EnvelopeBuilder();
        for(Geometry g : gs){
        	ShapeFeature f = createFeature(g);
            fc.insert(f);
            f.setFeatureClass(fc);
            eb.mergeEnvelop(g.getEnvelop());
        }

        fc.setMetadata( new LayerMetadata("Geometry",gs.get(0).getGeometryType()));
        fc.setEnvelope(eb.toEnvelop());

        return new VectorLayer(fc);
    }
    private static ShapeFeature createFeature(Geometry g)
    {
    	ShapeFeature shp = new ShapeFeature();
        shp.setGeometry(g);
        return shp;
    }

    public static VectorLayer createFeatureLayer(List<Feature> fs)
    {
    	FeatureList fc = new FeatureList();
        EnvelopeBuilder eb = new EnvelopeBuilder();
        for (Feature f : fs)
        {
            fc.insert(f);
            eb.mergeEnvelop(f.getEnvelop());
        }

        fc.setMetadata (new LayerMetadata("Feature",GeometryType.GeometryCollection));
        fc.setEnvelope ( eb.toEnvelop());

        return new VectorLayer(fc);
    }
}
